package com.dz51.core.proxy;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class HibernateTemplateProxy {

	private TemplateContainer slaveTemplateContainer = null;
	private TemplateContainer masterTemplateContainer = null;

	public HibernateTemplate getMasterHibernateTemplate() {
		return this.masterTemplateContainer.getHibernateTemplate();
	}

	public HibernateTemplate getSlaveHibernateTemplate() {
		return this.slaveTemplateContainer.getHibernateTemplate();
	}

	public HibernateTemplateProxy(SlaveTemplateContainer slaveTemplateContainer, MasterTemplateContainer masterTemplateContainer) {
		super();
		this.slaveTemplateContainer = slaveTemplateContainer;
		this.masterTemplateContainer = masterTemplateContainer;
	}

	/**
	 * ------------------------ bulkUpdate ，使用master数据源
	 * ------------------------------
	 **/
	/** 测试通过 **/
	public int bulkUpdate(String queryString, Object... values) throws DataAccessException {
		return this.getMasterHibernateTemplate().bulkUpdate(queryString, values);
	}

	public int bulkUpdate(String queryString, Object value) throws DataAccessException {
		return this.getMasterHibernateTemplate().bulkUpdate(queryString, value);
	}

	public int bulkUpdate(String queryString) throws DataAccessException {
		return this.getMasterHibernateTemplate().bulkUpdate(queryString);
	}

	/**
	 * ------------------------ bulkUpdate ，使用master数据源
	 * ------------------------------
	 **/

	/**
	 * ------------------------ delete ，使用master数据源
	 * ------------------------------
	 **/
	/** 测试通过 **/
	public void delete(Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().delete(entity, lockMode);
	}

	public void delete(Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().delete(entity);
	}

	public void delete(String entityName, Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().delete(entityName, entity, lockMode);
	}

	public void delete(String entityName, Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().delete(entityName, entity);
	}

	public void deleteAll(Collection entities) throws DataAccessException {
		this.getMasterHibernateTemplate().deleteAll(entities);
	}

	/**
	 * ------------------------ delete ，使用master数据源
	 * ------------------------------
	 **/

	/**
	 * ------------------------ execute ，使用master数据源
	 * ------------------------------
	 **/
	public <T> T execute(HibernateCallback<T> action) throws DataAccessException {
		return this.getMasterHibernateTemplate().execute(action);
	}

	/**
	 * ------------------------ execute ，使用master数据源
	 * ------------------------------
	 **/

	/**
	 * ------------------------ find-query-load-get ，使用slave数据源
	 * ------------------------------
	 **/
	/** 测试通过 **/
	public List find(String queryString, Object... values) throws DataAccessException {
		return this.getSlaveHibernateTemplate().find(queryString, values);
	}

	public List find(String queryString, Object value) throws DataAccessException {
		return this.getSlaveHibernateTemplate().find(queryString, value);
	}

	public List find(String queryString) throws DataAccessException {
		return this.getSlaveHibernateTemplate().find(queryString);
	}

	/** 测试通过 **/
	public List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
	}

	public List findByCriteria(DetachedCriteria criteria) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByCriteria(criteria);
	}

	/** 测试通过 **/
	public List findByExample(Object exampleEntity, int firstResult, int maxResults) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByExample(exampleEntity, firstResult, maxResults);
	}

	public List findByExample(Object exampleEntity) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByExample(exampleEntity);
	}

	public List findByExample(String entityName, Object exampleEntity, int firstResult, int maxResults) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByExample(entityName, exampleEntity, firstResult, maxResults);
	}

	public List findByExample(String entityName, Object exampleEntity) throws DataAccessException {
		return this.getSlaveHibernateTemplate().findByExample(entityName, exampleEntity);
	}

	/** 以下未测试，如有需要可以自行测试 **/
	// public List findByNamedParam(String queryString, String paramName, Object
	// value) throws DataAccessException {
	// return this.getSlaveHibernateTemplate().findByNamedParam(queryString,
	// paramName, value);
	// }
	// public List findByNamedParam(String queryString, String[] paramNames,
	// Object[] values) throws DataAccessException {
	// return this.getSlaveHibernateTemplate().findByNamedParam(queryString,
	// paramNames, values);
	// }
	// public List findByNamedQuery(String queryName, Object... values) throws
	// DataAccessException {
	// return this.getSlaveHibernateTemplate().findByNamedQuery(queryName,
	// values);
	// }
	// public List findByNamedQuery(String queryName, Object value) throws
	// DataAccessException {
	// return this.getSlaveHibernateTemplate().findByNamedQuery(queryName,
	// value);
	// }
	// public List findByNamedQuery(String queryName) throws DataAccessException
	// {
	// return this.getSlaveHibernateTemplate().findByNamedQuery(queryName);
	// }
	// public List findByNamedQueryAndNamedParam(String queryName, String
	// paramName, Object value) throws DataAccessException {
	// return
	// this.getSlaveHibernateTemplate().findByNamedQueryAndNamedParam(queryName,
	// paramName, value);
	// }
	// public List findByNamedQueryAndNamedParam(String queryName, String[]
	// paramNames, Object[] values) throws DataAccessException {
	// return
	// this.getSlaveHibernateTemplate().findByNamedQueryAndNamedParam(queryName,
	// paramNames, values);
	// }
	// public List findByNamedQueryAndValueBean(String queryName, Object
	// valueBean) throws DataAccessException {
	// return
	// this.getSlaveHibernateTemplate().findByNamedQueryAndValueBean(queryName,
	// valueBean);
	// }
	// public List findByValueBean(String queryString, Object valueBean) throws
	// DataAccessException {
	// return this.getSlaveHibernateTemplate().findByValueBean(queryString,
	// valueBean);
	// }

	/** 测试通过 **/
	public <T> T get(Class<T> entityClass, Serializable id, LockMode lockMode) throws DataAccessException {
		return this.getSlaveHibernateTemplate().get(entityClass, id, lockMode);
	}

	public <T> T get(Class<T> entityClass, Serializable id) throws DataAccessException {
		return this.getSlaveHibernateTemplate().get(entityClass, id);
	}

	public Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException {
		return this.getSlaveHibernateTemplate().get(entityName, id, lockMode);
	}

	public Object get(String entityName, Serializable id) throws DataAccessException {
		return this.getSlaveHibernateTemplate().get(entityName, id);
	}

	/** 因为跨session，不能使用延迟加载 **/
	// public <T> T load(Class<T> entityClass, Serializable id, LockMode
	// lockMode) throws DataAccessException {
	// return this.getSlaveHibernateTemplate().load(entityClass, id, lockMode);
	// }
	// public <T> T load(Class<T> entityClass, Serializable id) throws
	// DataAccessException {
	// return this.getSlaveHibernateTemplate().load(entityClass, id);
	// }
	// public void load(Object entity, Serializable id) throws
	// DataAccessException {
	// this.getSlaveHibernateTemplate().load(entity, id);
	// }
	// public Object load(String entityName, Serializable id, LockMode lockMode)
	// throws DataAccessException {
	// return this.getSlaveHibernateTemplate().load(entityName, id, lockMode);
	// }
	// public Object load(String entityName, Serializable id) throws
	// DataAccessException {
	// return this.getSlaveHibernateTemplate().load(entityName, id);
	// }
	/** 测试通过 **/
	public <T> List<T> loadAll(Class<T> entityClass) throws DataAccessException {
		return this.getSlaveHibernateTemplate().loadAll(entityClass);
	}

	/**
	 * ------------------------ find-query-load-get ，使用slave数据源
	 * ------------------------------
	 **/

	/**
	 * ------------------------
	 * lock-merge-persist-refresh-replicate-save-saveOrUpdate
	 * -saveOrUpdateAll-update ，使用master数据源 ------------------------------
	 **/
	public void lock(Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().lock(entity, lockMode);
	}

	public void lock(String entityName, Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().lock(entityName, entity, lockMode);
	}

	public <T> T merge(String entityName, T entity) throws DataAccessException {
		return this.getMasterHibernateTemplate().merge(entityName, entity);
	}

	public <T> T merge(T entity) throws DataAccessException {
		return this.getMasterHibernateTemplate().merge(entity);
	}

	/** 以下方法未详尽测试，如有需要可以自行测试 **/
	// public void persist(Object entity) throws DataAccessException {
	// this.getMasterHibernateTemplate().persist(entity);
	// }
	// public void persist(String entityName, Object entity) throws
	// DataAccessException {
	// this.getMasterHibernateTemplate().persist(entityName, entity);
	// }
	//
	// public void refresh(Object entity, LockMode lockMode) throws
	// DataAccessException {
	// this.getMasterHibernateTemplate().refresh(entity, lockMode);
	// }
	// public void refresh(Object entity) throws DataAccessException {
	// this.getMasterHibernateTemplate().refresh(entity);
	// }
	// public void replicate(Object entity, ReplicationMode replicationMode)
	// throws DataAccessException {
	// this.getMasterHibernateTemplate().replicate(entity, replicationMode);
	// }
	// public void replicate(String entityName, Object entity, ReplicationMode
	// replicationMode) throws DataAccessException {
	// this.getMasterHibernateTemplate().replicate(entityName, entity,
	// replicationMode);
	// }

	/** 存在跨session重新insert风险，改用saveOrUpdate **/
	// public Serializable save(Object entity) throws DataAccessException {
	// return this.getMasterHibernateTemplate().save(entity);
	// }
	// public Serializable save(String entityName, Object entity) throws
	// DataAccessException {
	// return this.getMasterHibernateTemplate().save(entityName, entity);
	// }
	/** 测试通过 **/
	public void saveOrUpdate(Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().saveOrUpdate(entity);
	}

	public void saveOrUpdate(String entityName, Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().saveOrUpdate(entityName, entity);
	}

	public void saveOrUpdateAll(Collection entities) throws DataAccessException {
		this.getMasterHibernateTemplate().saveOrUpdateAll(entities);
	}

	/** 测试通过 **/
	public void update(Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().update(entity, lockMode);
	}

	public void update(Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().update(entity);
	}

	public void update(String entityName, Object entity, LockMode lockMode) throws DataAccessException {
		this.getMasterHibernateTemplate().update(entityName, entity, lockMode);
	}

	public void update(String entityName, Object entity) throws DataAccessException {
		this.getMasterHibernateTemplate().update(entityName, entity);
	}
}
